/*
 * CLASS: UserSearchResult.java
 */

package view;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import model.*;

/**
 *
 * @author Samuele Mattiuzzo
 */
public class UserSearchResult extends javax.swing.JFrame {

    Library library;
    User user;
    ArrayList<Product> list;
    UserSearch search;
    
    /** Creates new form UserSearchResult */
    public UserSearchResult(Library l, User u, ArrayList<Product> p, UserSearch m) {
        library = l;
        user = u;
        list = p;
        search = m;
        initComponents();
        refreshTable();
        if(user.isSuperuser())
            jButtonBook.setVisible(false);
    }

    private void tableReset(int size){
        DefaultTableModel tbl = (DefaultTableModel)jTable2.getModel();
        for(int i = 0;i < size;i++)
            tbl.removeRow(0);
    }
    /**
     * Aggiorna la tabella degli autori
     */
    private void refreshTable(){
        int size = jTable2.getRowCount();
        DefaultTableModel tbl = (DefaultTableModel)jTable2.getModel();
        tableReset(size);
        for(Product a:list){                                
                int id = a.getProductId();
                String title = a.getTitle();
                String author = a.getAuthors().toString();
                String type = a.getType();
                String avail = a.getRented();
                tbl.addRow(new Object[]{id,title,author,type,avail});
    
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jButtonDetails = new javax.swing.JButton();
        jButtonBook = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButtonClose = new javax.swing.JButton();

        jDialog1.setTitle("");
        jDialog1.setBounds(new java.awt.Rectangle(0, 22, 300, 350));
        jDialog1.setResizable(false);

        org.jdesktop.layout.GroupLayout jDialog1Layout = new org.jdesktop.layout.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                .addContainerGap())
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
                .addContainerGap())
        );

        setTitle("RISULTATO RICERCA");

        jButtonDetails.setText("DETTAGLI");
        jButtonDetails.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDetailsMouseClicked(evt);
            }
        });

        jButtonBook.setText("PRENOTA");
        jButtonBook.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonBookMouseClicked(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "TITOLO", "AUTORE", "TIPO", "DISPONIBILE"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable2.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(jTable2);
        jTable2.getColumnModel().getColumn(0).setResizable(false);
        jTable2.getColumnModel().getColumn(1).setResizable(false);
        jTable2.getColumnModel().getColumn(2).setResizable(false);
        jTable2.getColumnModel().getColumn(3).setResizable(false);
        jTable2.getColumnModel().getColumn(4).setResizable(false);

        jButtonClose.setText("INDIETRO");
        jButtonClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCloseMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jButtonDetails)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonBook)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 264, Short.MAX_VALUE)
                        .add(jButtonClose)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButtonDetails)
                            .add(jButtonBook))
                        .addContainerGap(34, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonClose)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Prenota un prodotto
     * @param evt
     */
    private void jButtonBookMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonBookMouseClicked

        if(jTable2.getSelectedRow() > -1){
            Integer i = (Integer) jTable2.getModel().getValueAt(jTable2.getSelectedRow(), 0);
            library.getProducts().get(i).addBooking(new Booking(user));
            jLabel1.setText("Prenotazione effettuata");
            jDialog1.setVisible(true);
        refreshTable();
        }
    }//GEN-LAST:event_jButtonBookMouseClicked

    /**
     * Visiona i dettagli di un prodotto
     * @param evt
     */
    private void jButtonDetailsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDetailsMouseClicked
        // TODO add your handling code here:
        if(jTable2.getSelectedRow() > -1){
            Integer i = (Integer) jTable2.getModel().getValueAt(jTable2.getSelectedRow(), 0);
            jLabel1.setText(library.getProducts().get(i).displayInfo());
            jDialog1.setVisible(true);
        }
    }//GEN-LAST:event_jButtonDetailsMouseClicked

    /**
     * Chiude la finestra corrente
     * @param evt
     */
    private void jButtonCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCloseMouseClicked
        this.setVisible(false);
        search.setVisible(true);
    }//GEN-LAST:event_jButtonCloseMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBook;
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonDetails;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables

}
